package fr.umlv.ig.MetroView;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.Timer;

import fr.umlv.ig.model.Line;
import fr.umlv.ig.model.MetroModelImpl;
import fr.umlv.ig.model.Train;

@SuppressWarnings("serial")
public class MetroMapTrains extends JPanel {

	private final MetroModelImpl metroModel;
	public final Timer timer;
	private List<JTrain> listJTrains;
	
	public MetroMapTrains(MetroModelImpl metroModel) {
		this.metroModel = metroModel;
		this.listJTrains = new ArrayList<JTrain>();
		
		ActionListener moveTrains = new ActionListener () {
			@Override
			public void actionPerformed(ActionEvent e) {
				for(JTrain jtrain : listJTrains){
					jtrain.move();
					jtrain.revalidate();
				}
			}
		};
		this.timer = new Timer(100, moveTrains);
		
		setLayout(null);
	}

	public void generateTrain() {
		Line metroLine = metroModel.getLine(0);
		Train train = new Train(1, "MyFirstTrain", metroLine.getItinerary(0));
		metroModel.addTrain(metroLine, train);
	}
	
	public void initJTrains() {
		for(int i=0; i<metroModel.getLineCount(); i++) {
			Line metroLine = metroModel.getLine(i);
			for(int j=0; j<metroLine.getCountTrains(); j++) {
				Train train = metroLine.getTrain(j);
				JTrain jtrain = new JTrain(train);
				jtrain.setBounds(train.getCoordinate().x, train.getCoordinate().y, 10,10);
				jtrain.drawTrain();
				add(jtrain);
				listJTrains.add(jtrain);
				repaint();
				revalidate();
			}
		}
	}


	/*public void addTestTrains() {
		for(int i=0; i<10; i++) {
			Train train = new Train(i,"train"+i);
			Point p = new Point(i*3+100, i*5);
			System.out.println(p);
			train.setCoordinate(p);
			JTrain jtrain = new JTrain(train);
			jtrain.setBounds(train.getCoordinate().x, train.getCoordinate().y, 10,10);
			jtrain.drawTrain();
			add(jtrain);
			repaint();
			revalidate();
		}
	}*/
	
//	public void addTrain() {
//		Train train = new Train(1,"train1");
//		Point p = new Point(100, 300);
//		train.setCoordinate(p);
//		System.out.println(train.getCoordinate());
//		jtrain = new JTrain(train);
//		//jtrain.setBounds(100, 300, 10, 10);
//		jtrain.drawTrain();
//		add(jtrain);
//		repaint();
//		revalidate();
//		
//	}

}
